package com.qdu.impl;

import com.qdu.dao.CommontDao;
import com.qdu.entity.bookcomment;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * @author SBH
 */
public class CommontImpl implements CommontDao {

    @Override
    public int addCommon(String bookcomment_content, String bookcomment_sender, int bookcomment_bookid) {

        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0;

        Random random = new Random();

        int r = random.nextInt(100);

        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            ps = con.prepareStatement("insert into bookcomment values(?,?,?,?,?)");
            ps.setInt(1, r);
            ps.setString(2, bookcomment_content);
            ps.setString(3, bookcomment_sender);
            ps.setString(4, new java.util.Date().toLocaleString());
            ps.setInt(5, bookcomment_bookid);
            rows = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);

        }
        return rows;

    }

    @Override
    public int delCommon(int id) {

        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0;

        try {
            con = DatabaseUtil.getConnection();
            con.commit();

            ps = con.prepareStatement("delete bookcomment where bookcomment_id=?");

            ps.setInt(1, id);
            rows = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
    }

    @Override
    public List getCommont(int bookcomment_id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();

        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            ps = con.prepareStatement("select bookcomment.bookcomment_content,bookcomment.bookcomment_sender,bookcomment.bookcomment_sendtime,bookinformation.book_name from bookcomment,bookinformation where bookcomment.bookcomment_bookid=bookinformation.book_id and bookcomment.bookcomment_bookid=? order by bookcomment.bookcomment_id desc");
            ps.setInt(1, bookcomment_id);
            rs = ps.executeQuery();
            while (rs.next()) {
                List list2 = new ArrayList();
                list2.add(rs.getString(1));
                list2.add(rs.getString(2));
                list2.add(rs.getString(3));
                list2.add(rs.getString(4));
                list.add(list2);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;

    }

    @Override
    public String getBookName(int book_id) {
        String book_name = "";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();

        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            ps = con.prepareStatement("select book_name from bookinformation where book_id=?");
            ps.setInt(1, book_id);
            rs = ps.executeQuery();
            if (rs.next()) {
                book_name = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return book_name;
    }

    @Override
    public List getMessage(int page, String sql) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            con.commit();
            while (rs.next()) {
                List list2 = new ArrayList();
                list2.add(rs.getString(1));
                list2.add(rs.getString(2));
                list2.add(rs.getString(3));
                list2.add(rs.getString(4));
                list2.add(rs.getString(5));
                list.add(list2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public List getallmessage() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select bookcomment_content,bookcomment_sender,bookcomment_sendtime,bookcomment_bookid from bookcomment");
            rs = ps.executeQuery();
            con.commit();
            while (rs.next()) {
                List list4 = new ArrayList();
                list4.add(rs.getString(1));
                list4.add(rs.getString(2));
                list4.add(rs.getString(3));
                list4.add(rs.getInt(4));
                list.add(list4);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

}
